package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.SimpleAdapter;
import android.widget.Spinner;
import android.widget.Toast;

import com.example.utils.ToastUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class HighActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener {

    private Spinner sp_dropdown;
    private Spinner sp_dialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_high);


        spinnerDropdown();
        spinnerDialog();
        simpleSpinner();
    }
    //初始化数据
    private final static String[] startArray = {"水星","金星","地球","火星","木星","土星"};
    /**
     * 下拉模式列表
     */
    private void spinnerDropdown(){
        sp_dropdown = findViewById(R.id.sp_dropdown);
        //声明一个适配器,有三个参数
        //1.上下文
        //2.layout资源文件
        //3.源数据
        ArrayAdapter<String> adapter = new ArrayAdapter<>(this,R.layout.spinner_item_list,startArray);
        sp_dropdown.setAdapter(adapter);//设置adapter
        sp_dropdown.setSelection(0);//设置默认选中
        sp_dropdown.setOnItemSelectedListener(this);//设置监听item
    }

    /**
     * 弹窗dialog模式列表
     */
    private void spinnerDialog(){
        sp_dialog = findViewById(R.id.sp_dialog);
        //声明一个适配器
        ArrayAdapter<String> adapter2 = new ArrayAdapter<>(this,R.layout.spinner_item_list,startArray);
        sp_dialog.setAdapter(adapter2);//设置adapter
        sp_dialog.setSelection(0);//设置默认选中
        sp_dialog.setPrompt("请选择行星");
        sp_dialog.setOnItemSelectedListener(this);//设置监听item
    }




    //初始化数据
    private static final int[] iconArray = {
            R.drawable.sun, R.drawable.sun2, R.drawable.sun,
            R.drawable.sun, R.drawable.sun2, R.drawable.sun,
    };
    private static final String[] startArray2 = {"水星","金星","地球","火星","木星","土星"};
    /**
     * simpleSpinner列表
     */
    private void simpleSpinner(){
        List<Map<String,Object>> list = new ArrayList<>();//声明一个映射对象
        for (int i = 0; i < iconArray.length; i++) {
            Map<String,Object> item = new HashMap<>();
            item.put("icon", iconArray[i]);
            item.put("name", startArray2[i]);
            list.add(item);
        }
        SimpleAdapter simpleAdapter = new SimpleAdapter(this,list,R.layout.item_simple,
                new String[]{"icon","name"},
                new int[]{R.id.iv_icon,R.id.tv_name});

        Spinner sp_icon = findViewById(R.id.sp_icon);
        sp_icon.setAdapter(simpleAdapter);//设置adapter
        sp_icon.setOnItemSelectedListener(this);//设置监听item

    }
    /**
     *监听方法
     * @param parent 发生选择的AdapterView
     * @param view AdapterView中单击的视图
     * @param position 视图在适配器中的位置
     * @param id 所选项目的行id
     */
    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
        ToastUtils.show(this,"你选择的是:"+startArray[position]);
    }

    @Override
    public void onNothingSelected(AdapterView<?> parent) {

    }


}